package data;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: qiukexu
 * Date: 2009-6-5
 * Time: 10:21:51
 * Company: Shanghai Fastech Co.,Ltd.
 */
public class FileData implements FData{
    public static final int FILE_NAME_LENGTH = 50;
    public static final int FILE_NUM_LENGTH = 4;
    public static final int FILE_STATE_LENGTH = 4;
    
    private final static int DATA_TYPE = FData.DATA_FILE;

    private UUIDData uuidData;
    private int num;
    private int state;
    private String fileName;
    private byte[] filePieces;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public UUIDData getUuidData() {
        return uuidData;
    }

    public void setUuidData(UUIDData uuidData) {
        this.uuidData = uuidData;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public byte[] getFilePieces() {
        return filePieces;
    }

    public void setFilePieces(byte[] filePieces) {
        this.filePieces = filePieces;
    }

    public int getPackLeng() {
        return uuidData.getPackLeng()+4+4+50+filePieces.length;
    }

    public byte[] toBinaryData() {
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(buf);
        byte[] output = new byte[getPackLeng()];
        System.arraycopy(getUuidData().toBinaryData(), 0, output, 0, getUuidData().getPackLeng());
        try{
            out.writeInt(getNum());
            System.arraycopy(buf.toByteArray(), 0, output, getUuidData().getPackLeng(), FILE_NUM_LENGTH);
            buf.reset();
            out.writeInt(getState());
            System.arraycopy(buf.toByteArray(), 0, output, getUuidData().getPackLeng()+FILE_NUM_LENGTH, FILE_STATE_LENGTH);
            byte[] fileNameBytes = fileName.getBytes();
            if(fileNameBytes.length < FILE_NAME_LENGTH){
                System.arraycopy(new byte[FILE_NAME_LENGTH-fileNameBytes.length], 0, output, getUuidData().getPackLeng()+FILE_NUM_LENGTH+FILE_STATE_LENGTH, FILE_NAME_LENGTH-fileNameBytes.length);
            }
            System.arraycopy(fileNameBytes, 0, output, getUuidData().getPackLeng()+FILE_NUM_LENGTH+FILE_STATE_LENGTH+FILE_NAME_LENGTH-fileNameBytes.length, fileNameBytes.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.arraycopy(getFilePieces(), 0, output, getUuidData().getPackLeng()+FILE_NUM_LENGTH+FILE_STATE_LENGTH+FILE_NAME_LENGTH, getFilePieces().length);
        return output;
    }

    public int getDataType() {
        return DATA_TYPE;
    }

    public void cleanData() {
        filePieces = new byte[4];
    }
}
